package com.jeenms.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import com.jeenms.test.user.ISysUserService;
import com.jeenms.test.user.entity.SysUserEntity;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Autowired
	private ISysUserService sysUserService;

	@Component
	public class MyStartupRunner implements CommandLineRunner {

		@Override
		public void run(String... args) throws Exception {
			for (int i = 0; i < 4; i++) {
				new Thread() {
					public void run() {
						while (true) {
							insertBatch();
							System.out.println("insert Batch successfully");
							try {
								Thread.sleep(10);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
		}
	}

	private void insertBatch() {
		try {

			List<SysUserEntity> userList = new ArrayList<>();
			for (int i = 0; i < 3000; i++) {
				SysUserEntity sysUserEntity = new SysUserEntity();
				sysUserEntity.setUsername("name" + new Random().nextInt(1000));
				sysUserEntity.setSex(new Random().nextInt(2));
				userList.add(sysUserEntity);
			}
			sysUserService.insertBatch(userList, 500);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
